<template>
  <div class="app-container">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
    >
      <!-- 商品名字 -->
      <el-form-item prop="name" label="商品分类">
        <el-input placeholder="请输入商品的分类" v-model="ruleForm.name" />
      </el-form-item>
      <!-- 主图 上传文件 -->
      <el-form-item label="分类主图"
        ><el-upload
          class="avatar-uploader"
          :action="uploadUrl"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
        >
          <img v-if="imageUrl" :src="imageUrl | dalImg" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i> </el-upload
      ></el-form-item>
      <!-- 商品价格 -->
      <el-form-item prop="desc" label="分类描述">
        <el-input
          type="textarea"
          placeholder="请输入商品的描述"
          v-model="ruleForm.desc"
        />
      </el-form-item>

      <!-- 创建和重置按钮 -->
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >立即创建</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {
  addModelAPI,
  loadModelByIdAPI,
  updataModelByIdAPI
} from '@/api/product-categories'
import { uploadUrl } from '@/utils/request'
export default {
  data() {
    return {
      ruleForm: {
        name: '',
        desc: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      },
      imageUrl: '',
      uploadUrl
    }
  },
  async created() {
    // id 存在调接口取数据，id不存在什么都不做
    if (this.$route.query.id) {
      // 点击修改的时候把原有的数据带过来
      const res = await loadModelByIdAPI(this.$route.query.id)
      this.ruleForm.name = res.name
      this.ruleForm.desc = res.desc
      this.imageUrl = res.coverImage
    }
  },
  methods: {
    // 创建
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          // 判断是修改还是新增
          if (this.$route.query.id) {
            // id 存在 调用修改api
            await updataModelByIdAPI(this.$route.query.id, { ...this.ruleForm })
            this.$message.success('修改成功')
          } else {
            // id 不存在 调用新增api
            await addModelAPI({ ...this.ruleForm, coverImage: this.imageUrl })
            this.$message.success('创建成功')
          }
          // this.$message.success('创建成功')
          this.$router.push({
            name: 'ProductCategoryList'
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 重置
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    // 文件上成功后的回调
    handleAvatarSuccess(res, file) {
      this.imageUrl = res.data
      // console.log(res)
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
